package cn.xiaopengstack.domain.pay.factory;

import cn.xiaopengstack.domain.pay.service.IPayService;
import cn.xiaopengstack.types.enums.PayChannelEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author jiangyangang
 */
@Component
@Slf4j
public class PayServiceFactory {

    private static Map<PayChannelEnum, IPayService> PAY_SERVICE_MAP = new HashMap<>();

    public PayServiceFactory(List<IPayService> payServices){
        if (!CollectionUtils.isEmpty(payServices)) {
            PAY_SERVICE_MAP = payServices.stream().collect(Collectors.toMap(IPayService::channel, Function.identity()));
        }
    }

    /**
     *  获取处理方式
     * @param payChannelEnum
     * @return
     */
    public Optional<IPayService> getPayResultService(PayChannelEnum payChannelEnum){
        IPayService payService = PAY_SERVICE_MAP.get(payChannelEnum);
        if (Objects.isNull(payService)) {
            log.warn("获取支付结果处理服务失败，没有当前支付方式的处理器:{}", payChannelEnum.name());
        }

        return Optional.of(payService);
    }
}
